import { getHomeDate, getArouth, getPostsInfo, addCollection, addLook, getUserCollectOrCommitData, removeCollectionOrComment, addCollectOrCommentNum, removeCollectOrCommentNum, search } from '@/http/requestFunction'

export default {
    namespaced: true,
    actions: {
        // 获取页面数据
        async getHomeDate(content, type) {
            try {
                const res = await getHomeDate(type)
                content.commit('GETHOMEDATA', res.data.list)
            } catch (e) {
                console.log(e)
            }
        },

        // 查询作者信息
        async getArouth(content, _id) {
            try {
                const res = await getArouth(_id)
                content.commit('GETAROUTHINFO', res.data.username)

            } catch (e) {
                console.log(e)
            }
        },
        // 获取详情数据
        async getPostsInfo(content, _id) {
            try {
                const res = await getPostsInfo(_id)
                content.commit('GETPOSTSINFO', res.data.doc)
            } catch (e) {
                console.log(e)
            }
        },

        // 添加点赞收藏
        async addCollection(content, info) {
            try {
                const res = await addCollection(info.item, info.type)
            } catch (e) {
                console.log(e)
            }
        },

        // 添加浏览量
        async addLook(content, _id) {
            try {
                const res = await addLook(_id)
            } catch (e) {
                console.log(e)
            }
        },

        // 获取收藏数据
        async getUserCollectOrCommitData(content, type) {
            try {
                const res = await getUserCollectOrCommitData(type)
                content.commit('USERCOLLECTLIST', res.data.myCollection)
            } catch (e) {
                console.log(e)
            }
        },
        // 获取点赞数据
        async getUserCommentData(content, type) {
            const res = await getUserCollectOrCommitData(type)
            content.commit('USERCOMMENTLIST', res.data.myCollection)
        },

        // 取消点赞和收藏
        async removeCollectionOrComment(content, val) {
            try {
                const res = await removeCollectionOrComment(val.type, val.item)
            } catch (e) {
                console.log(e)
            }
        },

        // 增加收藏量和点赞量
        async addCollectOrCommentNum(content, info) {
            try {
                const res = await addCollectOrCommentNum(info.type, info.item)
            } catch (e) {
                console.log(e)
            }
        },

        // 减少收藏量和点赞量
        async removeCollectOrCommentNum(content, info) {
            try {
                const res = await removeCollectOrCommentNum(info.type, info.item)
            } catch (e) {
                console.log(e)
            }
        },

        // 模糊搜索
        async search(content, info) {
            try {
                const res = await search(info)
                content.commit('SEARCHDATE',res.data.list)
            } catch (e) {
                console.log(e)
            }
        }
    },
    mutations: {
        // 获取页面数据
        GETHOMEDATA(state, list) {
            state.HomeList = list
        },

        // 查询作者信息
        GETAROUTHINFO(state, val) {
            state.arouthInfo = val
        },
        // 获取详情页信息
        GETPOSTSINFO(state, PostsInfo) {
            state.PostsInfo = PostsInfo
        },
        // 保存用户收藏的数据
        USERCOLLECTLIST(state, list) {
            state.userCollectList = list
        },
        // 保存用户点赞的数据
        USERCOMMENTLIST(state, list) {
            state.userCommentList = list
        },
        SEARCHDATE(state,list){
            state.searchDate = list
        }
    },
    state: {
        HomeList: [],
        arouthInfo: "",
        PostsInfo: {},
        userCollectList: [],
        userCommentList: [],
        searchDate:[]

    }

}